package com.lei.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.lei.msmservice.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.HashMap;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(HashMap<String, Object> param, String tel) {
        if (StringUtils.isEmpty(tel)){return false;}
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4G5tByz7DU1CBD7kQeDk", "NmkHfjCN8B0VUbWfBV7GgdK5L2omzh");
        IAcsClient client = new DefaultAcsClient(profile);

        //固定参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //相关参数
        request.putQueryParameter("PhoneNumbers",tel); //手机号
        request.putQueryParameter("SignName","Doream教育网站"); //申请阿里云 签名名称
        request.putQueryParameter("TemplateCode","SMS_193235297"); //申请阿里云 模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递


        //最终发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

    }
}
